using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SerialCommand;

namespace DataLogger {
    public partial class Form1 : Form {

        private CommandCore _core = null;
        

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {

            // COM ports
            string[] ports = CommandCore.GetComPortNames();
            foreach (string port in ports) {
                this.cbComPorts.Items.Add(port);
            }


            // Device Commands
            Devices.Schema.Device dev;
            dev = CommandCore.GetDeviceSettings(@"Devices\GSM\Generic.xml");
            foreach (Devices.Schema.Command cmd in dev.Commands) {
                cbDeviceCommands.Items.Add(new DeviceCommand(cmd));
            }
        }



        void _core_ErrorEvent(string message) {
            MessageBox.Show("ERROR: " + message);
        }

        void _core_DataRecvEvent(string data) {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.listBox1.InvokeRequired) {
                CommandCore.DataRecvHandler d = new CommandCore.DataRecvHandler(_core_DataRecvEvent);
                this.Invoke(d, new object[] { data });
            } else {
                this.listBox1.Items.Add(data);
            }

        }



        private void buttonConnect_Click(object sender, EventArgs e) {
            if (_core == null) {
                _core = new CommandCore(this.cbComPorts.SelectedText, 19200, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                buttonConnect.Text = "Disconnect";
            } else {
                _core = null;
                buttonConnect.Text = "Connect";
            }
        }

        private void cbDeviceCommands_SelectedIndexChanged(object sender, EventArgs e) {
            DeviceCommand cmd = (DeviceCommand)this.cbDeviceCommands.SelectedItem;
            //this.propertyGrid1.SelectedObject = cmd.BaseCommand.ParamaterCollection;
            this.listView1.DataBindings = cmd.BaseCommand.ParamaterCollection;
        }

        private void cbComPorts_SelectedIndexChanged(object sender, EventArgs e) {

        }
    }
}